<template>
	<view>

		<text class="secret-info" v-if="transform!='translateY(0)'">零零零零!</text>

		<view class="welcome-container" @touchstart="touchStart" @touchmove="touchMove" @touchend="touchEnd"
			:style="welcomeStyle">
			<image src="../../static/logo/icon.png" class="ic-welcome"></image>
			<text class="welcome">欢迎加入一点不通!</text>
			<text class="sub-welcome"></text>
			<view class="upload-container" @tap="tapToChoose">
				<image :src="avatar" mode="aspectFill" class="user-avatar"></image>
				<text class="input-holder">{{avatarUrl}}</text>
			</view>
			<input type="text" class="input-input" placeholder="请输入昵称" placeholder-class="input-holder" id="nick"
				@input="inputChange" />
			<input type="text" class="input-input" placeholder="请输入用户名" placeholder-class="input-holder" id="user"
				@input="inputChange" />
			<input type="text" class="input-input" placeholder="请输入邮箱" placeholder-class="input-holder" id="email"
				@input="inputChange" />
			<input type="text" class="input-input" placeholder="请输入电话号码" placeholder-class="input-holder" id="phone"
				@input="inputChange" />
			<view class="pwd-container">
				<input v-if="isPwd" type="password" class="input-pwd" placeholder-class="input-holder"
					placeholder="请输入密码" id="password" @input="inputChange" />
				<input v-else type="text" class="input-pwd" placeholder="请输入密码" placeholder-class="input-holder"
					id="password" @input="inputChange" />
				<text
					:class="isPwd?'iconfont icon-icon_login_input_password_default ic-pwd':'iconfont icon-icon_login_input_password_pressed ic-pwd' "
					@click="clickSwitchPwd"></text>
			</view>
			<view class="content-switch">
				<text class="switch-Text">申请教师</text>
				<switch type="switch" style="transform:scale(0.6)" color="red" @change="sChecked"></switch>
			</view>
			<text class="btn-register input-input" @click="clickRegister">注册</text>
		</view>

	</view>
</template>

<script>
	let startY = 0
	let pic = ''
	let ino = false
	export default {
		data() {
			return {
				transform: 'translateY(0)',
				transition: '',
				avatar: "../../static/logo/default_user.png",
				avatarUrl: "请上传图片",
				isPwd: true,
				nick: '',
				email: '',
				user: '',
				phone: '',
				password: '',
				apply: false,
			}
		},
		onLoad() {
			uni.setNavigationBarTitle({
				title: "注册用户"
			})
		},
		computed: {
			welcomeStyle: function() {
				return {
					'transform': this.transform,
					'transition': this.transition,
				}
			}
		},
		methods: {
			sChecked(e) {
				console.log(e.detail.value);
				ino = e.detail.value

			},
			touchStart(e) { //记录开始触摸点的纵坐标
				startY = e.touches[0].clientY
			},
			touchMove(e) {
				let distance = e.touches[0].clientY - startY
				if (distance >= 150) {
					distance = 150
				}
				if (distance < 0) {
					return
				}
				this.transform = `translateY(${distance}rpx)`
				// console.log(distance);
			},
			touchEnd(e) {
				this.transform = 'translateY(0)'
				this.transition = 'transform 500ms linear'
			},
			tapToChoose() {
				let url = this.$params.host + this.$params.action_upload

				uni.chooseImage({
					count: 1,
					success: res => {

						let path = res.tempFilePaths[0]
						this.$request.postFile(url, 'file', path, {}, res => {


							let json = JSON.parse(res.data)
							pic = json.data
							this.avatarUrl = pic
							this.avatar = this.$params.host + pic
						})
					}

				})
			},
			clickSwitchPwd() {
				this.isPwd = !this.isPwd
			},
			inputChange(e) {

				console.log(e.target.id);
				let prop = e.target.id
				this[`${prop}`] = e.detail.value
			},
			clickRegister() { //注册账号
				if (!this.checkValid()) {
					return
				}
				let data = {

					nick: this.nick,
					user: this.user,
					avatar: pic,
					password: this.password,
					email: this.email,
					phone: this.phone,
					apply: ino

				}
				console.log(data);

				let url = this.$params.host + this.$params.action_register
				this.$request.post(url, data, res => { //
					console.log(res);
					if (!res.success) {
						uni.showToast({
							title: res.message,
							icon: 'none'
						});
						return
					}
					uni.showToast({
						title: res.message,
						icon: 'none'
					});
					const app = getApp()
					app.globalData.uid = res.data.id
					app.globalData.avatar = res.data.avatar
					app.globalData.email = res.data.email
					app.globalData.userName = res.data.userName
					app.globalData.nickName = res.data.nickName
					app.globalData.type = res.data.type
					app.globalData.token = res.data.token
					app.globalData.phone = res.data.phone
					app.globalData.apply = res.data.apply

					this.$noti.post(this.$params.noti_login_status, true)
					// uni.navigateBack()
					uni.navigateTo({
						url:"/pages/index/index"
					})
				})
				console.log(data);
			},
			checkValid() { //验证信息
				if (!this.$utils.checkLength(this.nick, 2, "昵称")) {
					return false
				}
				if (!this.$utils.checkLength(this.user, 2, "用户名")) {
					return false
				}
				if (!this.$utils.checkLength(this.password, 6, "密码")) {
					return false
				}
				let regex = /^[a-zA-Z0-9\u4e00-\u9fa5_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
				if (!regex.test(this.email)) {
					uni.showToast({
						title: '邮箱格式不正确',
						icon: 'none'
					});
					return false
				}
				if (this.avatar == "../../static/logo/default_user.png") {
					uni.showToast({
						title: '图片不能为空',
						icon: 'none'
					});
					return false
				}
				return true
			},
		}
	}
</script>

<style>
	@import url("register.css");
</style>
